package ntd.util.frame.javacv.bo;

import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Entity;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.google.gson.Gson;
import lombok.Data;
import lombok.experimental.Accessors;
import ntd.util.config.common.enums.protocol.EnumStreamUrlProtocol;
import ntd.util.frame.haikang.remote.RemoteHaiKang;
import ntd.util.frame.haikang.tool.ToolHaiKangVideo;
import ntd.util.frame.javacv.HaikangRtspBean;

import java.util.List;

@Data
@Accessors(chain = true)
public class CameraBO {

    private String code;
    private String CameraName;//摄像头名称

    private String CameraMn;//摄像头编码

    private List<String> modeTypes;

    private String rstpUrl;

    public static CameraBO initCode(String code) {
        CameraBO cameraBO = new CameraBO().setCode(code);
        cameraBO.initRtspUrl();
        return cameraBO;
    }

    public String initRtspUrl() {
        RemoteHaiKang remoteHaiKang = ToolHaiKangVideo.initHaiKang("40289f5a7e3c994d017e3c998daf0000");
        String s = remoteHaiKang.getStreamUrlByCameraIndexCode(this.getCode(), EnumStreamUrlProtocol.PROTOCOL_RTSP.getProtocolName());
        HaikangRtspBean bean =  new Gson().fromJson(s, HaikangRtspBean.class);
        //设置rtsp地址
        this.setRstpUrl(bean.getData().getUrl());
        return this.getRstpUrl();
    }

    public static CameraBO transform(Entity entity) {
        if (ObjectUtil.isNull(entity)) {
            return null;
        }
        CameraBO cameraBO = new CameraBO();
        cameraBO.setCode(entity.getStr("code"));
        String config = entity.getStr("config");
        if (StrUtil.isBlank(config)) {
            return null;
        }
        if (!JSONUtil.isTypeJSONObject(config)) {
            return null;
        }
        JSONObject configObj = JSONUtil.parseObj(config);
        if (ObjectUtil.isNull(configObj)) {
            return null;
        }
        JSONObject aiVisionObj = configObj.getJSONObject("ai-vision");
        if (ObjectUtil.isNull(aiVisionObj)) {
            return null;
        }
        if (!aiVisionObj.getBool("open")) {
            return null;
        }
        JSONArray openModels = aiVisionObj.getJSONArray("open_models");
        List<String> openModelStrs = CollStreamUtil.toList(openModels, Object::toString);
        cameraBO.setModeTypes(openModelStrs);
        return cameraBO;
    }

}
